/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.multi.client.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.macdadi.core.client.domain.DataWriteService;
import org.macdadi.core.client.domain.ProjectInfo;
import org.macdadi.multi.client.Overview;

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class CreateProjectPanel extends Composite
{
    public static final String CSS_CLASS = "macdadi-CreateProjectPanel";

    protected TextBox name;
    protected TextBox urlPath;
    protected Button create;
    protected HTML feedback;

    public CreateProjectPanel()
    {
        VerticalPanel vp = new VerticalPanel();
        vp.add(new HTML("<h4>Create a new project</h4>"));

        name = new TextBox();
        urlPath = new TextBox();
        create = new Button("Create it!");
        create.addClickListener(new ClickListener() {
            public void onClick(Widget sender)
            {
                doCreateProject();
            }
        });
        feedback = new HTML();

        FlexTable table = new FlexTable();
        table.setWidget(0, 0, new HTML("Name"));
        table.setWidget(0, 1, name);
        table.setWidget(0, 2, new HTML("e.g. Stanford GSB"));
        table.setWidget(1, 0, new HTML("Link"));
        table.setWidget(1, 1, urlPath);
        table.setWidget(1, 2, new HTML("e.g. gsb1"));
        table.setWidget(2, 0, create);
        table.getFlexCellFormatter().setColSpan(2, 0, 3);
        table.setWidget(3, 0, feedback);
        table.getFlexCellFormatter().setColSpan(3, 0, 3);
        //table.getFlexCellFormatter().setHorizontalAlignment(3, 0, HasAlignment.ALIGN_CENTER);
        vp.add(table);

        initWidget(vp);
        setStyleName(CSS_CLASS);
    }

    protected void doCreateProject()
    {
        feedback.setText("");

        String nameVal, urlPathVal;
        nameVal = name.getText().trim();
        urlPathVal = urlPath.getText().trim();

        if ( nameVal.length() == 0 || urlPathVal.length() == 0 ) {
            doFeedback("Both inputs are required");
            return;
        }

        /* I used http://regex.powertoy.org/ for testing this regular expression */
        if ( ! urlPathVal.matches("[a-z0-9\\-]*")) {
            doFeedback("The link may consist only of lower-case letters, numbers and the dash - character");
            return;
        }

        DataWriteService.App.getInstance().createProject(nameVal, urlPathVal, -1, Overview.getCurrentAuthToken(), new AsyncCallback() {
            public void onFailure(Throwable caught) {
                GWT.log("Failed to create a new project", caught);
                doFeedback("An error occurred, we were unable to create the new project");
            }

            public void onSuccess(Object result) {
                ProjectInfo pi = (ProjectInfo) result;
                if ( pi != null ) {
                    String projectLink = "<a href='p?up=" + pi.getUrlPath() + "'>" + pi.getName() + "</a>";
                    doFeedback("A new project was created: " + projectLink);
                } else {
                    doFeedback("The project was not created, perhaps the name or link are already in use.");
                }
            }
        });
    }

    protected void doFeedback(String msg)
    {
        feedback.setHTML(msg);
    }
}